package shuiyuqing.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Spinner;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import inter.Delegate;
import inter.ResponseStatus;
import shuiyuqing.adapter.ChooseStationAdapter;
import shuiyuqing.adapter.HistoryDataAdapter;
import shuiyuqing.data.AllPoints;
import shuiyuqing.dto.DATA;
import shuiyuqing.dto.STINFO;
import shuiyuqing.network.GetHistoryHydInfo;
import shuiyuqing.network.GetRTHydInfo;
import utils.TimeUtil;

/**
 * 历史水情
 * Created by Administrator on 2016/3/22.
 */
public class HistoryActivity extends UIHandler.BaseActivity {


    Spinner sp_loc;
    List<STINFO> stinfos = new ArrayList<STINFO>();
    ChooseStationAdapter chooseStationAdapter = new ChooseStationAdapter();

    {
        chooseStationAdapter.setStations(stinfos);
    }


    // 选中的站点
    STINFO stinfo = null;

    // 选中的日期
    String date = TimeUtil.currentDate();

    // 历史数据
    List<GetHistoryHydInfo.RTData> datas = new ArrayList<GetHistoryHydInfo.RTData>();
    HistoryDataAdapter historyDataAdapter = new HistoryDataAdapter();
    {
        historyDataAdapter.setDatas(datas);
    }

    ListView lv_history;

    Button btn_date;

    Calendar calendar = Calendar.getInstance();
    int start_year=calendar.get(Calendar.YEAR),start_month=calendar.get(Calendar.MONTH),start_day=calendar.get(Calendar.DAY_OF_MONTH);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        initview();
        initdata();
    }

    private void initdata() {
        AllPoints.setNotifyGet(new AllPoints.NotifyGet() {
            @Override
            public void get(AllPoints allPoints) {
                stinfos.clear();
                stinfos.addAll(allPoints.getStinfos());
                chooseStationAdapter.notifyDataSetChanged();
            }
        });

        AllPoints allPoint = AllPoints.getAllPoints();

        if (allPoint != null) {
            stinfos.clear();
            stinfos.addAll(allPoint.getStinfos());
            chooseStationAdapter.notifyDataSetChanged();
        }

        btn_date.setText(start_year + "年" + (start_month + 1) + "月" + start_day + "日");
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_date)
            {
                selectDate();
            }
        }
    };

    // 选中日期
    private void selectDate() {
        // 日期选择器
        Calendar calendar = Calendar.getInstance();
        Dialog dialog;
        dialog = new DatePickerDialog(HistoryActivity.this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                date = year+"-"+(((monthOfYear+1)<=9&&(monthOfYear+1)>=1)?"0"+(monthOfYear+1):(1+monthOfYear)+"")+"-"+((dayOfMonth>=1&&dayOfMonth<=9)?"0"+dayOfMonth:dayOfMonth+"");
                start_year = year;
                start_month = monthOfYear;
                start_day = dayOfMonth;
                btn_date.setText(start_year + "年" + (start_month + 1) + "月" + start_day + "日");
                askdata();
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }


    private void initview() {

        btn_date = (Button) findViewById(R.id.id_btn_date);
        btn_date.setOnClickListener(onClickListener);

        sp_loc = (Spinner) findViewById(R.id.id_spinner_loc);
        sp_loc.setAdapter(chooseStationAdapter);
        sp_loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                select(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        lv_history = (ListView) findViewById(R.id.id_lv_history);
        lv_history.setEmptyView(findViewById(R.id.id_v_nonemsg));
        lv_history.setAdapter(historyDataAdapter);
    }

    // 选中站点
    private void select(int position) {
        stinfo = stinfos.get(position);
        askdata();

    }



    Delegate getHistoryData = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetHistoryHydInfo.ResponseHisHydInfo responseHisHydInfo = gson.fromJson(response, GetHistoryHydInfo.ResponseHisHydInfo.class);
            if (responseHisHydInfo.getStatus().equals(ResponseStatus.SUCCESS))
            {
                datas.clear();
                datas.addAll(responseHisHydInfo.getHisData());
                historyDataAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };


    // 访问历史数据
    public void askdata()
    {
        if (stinfo == null || date == null) return;

        GetHistoryHydInfo getHistoryHydInfo = new GetHistoryHydInfo();
        GetHistoryHydInfo.SearchHisData searchHisData = new GetHistoryHydInfo.SearchHisData();
        searchHisData.setSt_ID(stinfo.getSTID()+"");
        searchHisData.setDate(date);
        getHistoryHydInfo.setSearchHisData(searchHisData);
        getHistoryHydInfo.setDelegate(getHistoryData);
        getHistoryHydInfo.get();
    }
}
